package com.itcast.googleplay.ui;

import java.util.ArrayList;
import java.util.List;

import com.itcast.googleplay.utils.UIUtils;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;


public class FlowLayout2 extends ViewGroup {
	private static final String TAG = "FlowLayout2";
	public int horizontalSpacing = UIUtils.dip2px(10);
	public int verticalSpacing = UIUtils.dip2px(10);
	public List<Line> lineList = new ArrayList<FlowLayout2.Line>();
	
	public FlowLayout2(Context context) {
		this(context,null);
	}
	public FlowLayout2(Context context, AttributeSet attrs) {
		this(context, attrs,-1);
	}
	public FlowLayout2(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		int left = getPaddingLeft();
		int top = getPaddingTop();
		
		for (int i = 0; i < lineList.size(); i++) {
			Line line = lineList.get(i);
			line.layout(left, top);
			top += line.lineHeight+verticalSpacing; 
		}
	}
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		lineList.clear();
		
		int width = MeasureSpec.getSize(widthMeasureSpec);
		int usedWidth = width - getPaddingLeft() - getPaddingRight();
		
		Line line = new Line();
		for(int i=0;i<getChildCount();i++){
			View view = getChildAt(i);
			view.measure(0, 0);
			int childWidth = view.getMeasuredWidth();
			
			if(line.getLineViewCount() == 0){
				line.addView(view);
			}else if(childWidth+line.lineWidth+horizontalSpacing>usedWidth){
				//宽度大于可用宽度
				lineList.add(line);
				//换行添加view
				line = new Line();
				line.addView(view);
			}else{
				//添加view
				line.addView(view);
			}
			
			if(i == (getChildCount() - 1)){
				lineList.add(line);
			}
		}
		int height = getPaddingBottom()+getPaddingTop();
		for (int i = 0; i < lineList.size(); i++) {
			//获取行高度值大小
			height += lineList.get(i).lineHeight;
		}
		height += (lineList.size()-1)*verticalSpacing;
		
		Log.i(TAG, "height = "+height);
		
		widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
		heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
		
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}
	
	public class Line{
		public int lineWidth;
		public int lineHeight;
		//获取行集合
		public List<View> lineViewList = new ArrayList<View>();
		public int getLineViewCount(){
			return lineViewList.size();
		}
		
		public void addView(View view){
			lineViewList.add(view);
			
			view.measure(0, 0);
			//获取高度
			lineHeight = Math.max(view.getMeasuredHeight(), lineHeight);

			//获取已用的宽度
			if(lineViewList.size() == 1){
				lineWidth = view.getMeasuredWidth();
			}else{
				lineWidth += view.getMeasuredWidth() + horizontalSpacing;
			}
		
		}
		
		private void layout(int left,int top) {
			//剩余总宽度
			int totalSurplusWidth = getMeasuredWidth()-getPaddingLeft()-getPaddingRight()-lineWidth;
			//剩余中宽度均分给每一个view
			int surplusWidth = totalSurplusWidth/getLineViewCount();
			for (int i = 0; i < lineViewList.size(); i++) {
				View view = lineViewList.get(i);
				view.measure(0, 0);
				
				int width = view.getMeasuredWidth()+surplusWidth;
				
				int width32 = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
				int height32 = MeasureSpec.makeMeasureSpec(lineHeight, MeasureSpec.EXACTLY);
				
				view.measure(width32, height32);
				
				view.layout(left,top, left+width, top+lineHeight);
				left += width+horizontalSpacing; 
			}
		}
	}
}
